package com.jsyh.maidu.activity.me;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.BaseActivity;
import com.jsyh.maidu.config.ConfigValue;
import com.jsyh.maidu.config.SPConfig;
import com.jsyh.maidu.http.BaseDelegate;
import com.jsyh.maidu.http.ExceptionHelper;
import com.jsyh.maidu.http.OkHttpClientManager;
import com.jsyh.maidu.model.PayModel;
import com.jsyh.maidu.utils.KeyBoardUtils;
import com.jsyh.maidu.utils.SPUtils;
import com.jsyh.maidu.utils.Utils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sks on 2015/9/17.
 * 注册
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private Context context;
    private EditText editUserName, editPassword;
    private String mobile, password;
    private TextView btnRegister;
    TextView title;
    ImageView back;
    FrameLayout left;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        context = this;
        title = (TextView) findViewById(R.id.title);
        back = (ImageView) findViewById(R.id.back);
        left = (FrameLayout) findViewById(R.id.fl_Left);
        back.setBackgroundResource(R.mipmap.ic_back);
        title.setText("用户注册");
        left.setOnClickListener(this);
        editUserName = (EditText) findViewById(R.id.editUserName);
        editPassword = (EditText) findViewById(R.id.editPassword);
        btnRegister = (TextView) findViewById(R.id.btnRegister);
        editUserName.addTextChangedListener(textWatcher);
        editPassword.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mobile = editUserName.getText().toString().trim();
            password = editPassword.getText().toString().trim();
            if (!mobile.equals("") && !password.equals("")) {
                btnRegister.setTextColor(getResources().getColor(R.color.white));
                btnRegister.setBackgroundResource(R.drawable.login_on_bg);
                btnRegister.setClickable(true);
                btnRegister.setOnClickListener(RegisterActivity.this);
            } else {
                btnRegister.setTextColor(getResources().getColor(R.color.gary_text));
                btnRegister.setBackgroundResource(R.drawable.login_off_bg);
                btnRegister.setClickable(false);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                KeyBoardUtils.closeKeybord(this);
                break;
            case R.id.btnRegister:
                getRequest();
                break;
        }
    }

    private void getRequest() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobile", mobile);
        map.put("passwd", password);
        OkHttpClientManager.postAsyn(context, ConfigValue.APP_IP + "user/register",
                map, new BaseDelegate.ResultCallback<PayModel>() {
                    @Override
                    public void onError(com.squareup.okhttp.Request request, Object tag, Exception e) {
                        Utils.showToast(context, ExceptionHelper.getMessage(e, context));
                    }

                    @Override
                    public void onResponse(PayModel response, Object tag) {
                        if (response.getCode().equals("1")) {
                            String key = response.getData();
                            SPUtils.put(context, SPConfig.KEY, key);
                            ConfigValue.DATA_KEY = key;
                            KeyBoardUtils.closeKeybord(RegisterActivity.this);
                        }
                        Utils.showToast(context, response.getMsg());
                    }
                }, true);
    }
}
